﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using Helper;
using CoreData;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmStockBegin_Detail : DetailForm
    {
        public frmStockBegin_Detail()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable();
            dateEdit_create.Text = DateTime.Now.ToShortDateString();


            lookUpEdit_Warehouse.Properties.DataSource = RegisterService.RegisterServices.ProductWareHouseService.GetListByParent(" where ParentID = 0");
            lookUpEdit_Warehouse.Properties.ValueMember = "ID";
            lookUpEdit_Warehouse.Properties.DisplayMember = "Name";
            lookUpEdit_Warehouse.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Phòng"));
            lookUpEdit_Warehouse.EditValue = 1;
            lookUpEdit_Warehouse.Enabled = false;

            lookUpEdit_Staff.Properties.DataSource = RegisterService.RegisterServices.StaffService.GetList();
            lookUpEdit_Staff.Properties.ValueMember = "ID";
            lookUpEdit_Staff.Properties.DisplayMember = "Name";
            lookUpEdit_Staff.EditValue = ValueDefine.StaffID.ToString();
            lookUpEdit_Staff.Enabled = false;

            //txtInventoryNo.Text = RegisterService.RegisterServices.InventoryService.GetInventoryNo("TDK");

            //LoadInventory();

        }
        void LoadInventory()
        {
            IList<BeginStock_Detail> data = new List<BeginStock_Detail>();
            data = RegisterService.RegisterServices.BeginStock_DetailService.GetList_NewBegin();
            if (data != null)
            {
                gridControl1.DataSource = data;
                gridView1.RefreshData();
                SaveAble = true;
            }
            
        }

        private void lookUpEdit_Warehouse_EditValueChanged(object sender, EventArgs e)
        {            
            LoadInventory();
        }

        private void lookUpEdit_Warehouse_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            
        }

        private void lookUpEdit_Warehouse_CloseUp(object sender, DevExpress.XtraEditors.Controls.CloseUpEventArgs e)
        {
            
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gridView1.IsEditing)
            {
                if (e.Column.FieldName == "NewQuantity")
                {
                    int _Disparity = Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, "OldQuantity")) - Convert.ToInt32(gridView1.GetRowCellValue(e.RowHandle, "NewQuantity"));
                    if (_Disparity >= 0)
                    {
                        gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["DisparityExt"], _Disparity);
                       
                    }
                    else
                    {
                        gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["DisparityExt"], 0);
                        gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["NewQuantity"], gridView1.GetRowCellValue(e.RowHandle, "OldQuantity"));
                       
                    } 
                    gridView1.UpdateCurrentRow();
                    gridView1.RefreshData();
                }
            }
        }
        protected override long Save()
        {
            BeginStock data = new BeginStock();
            //data.BeginStockNo = txtInventoryNo.Text;
            data.BeginStockDate = dateEdit_create.DateTime;
            data.WarehouseID = (long)lookUpEdit_Warehouse.EditValue;
            data.StaffID = ValueDefine.StaffID;

            ID = RegisterService.RegisterServices.InventoryService.Save(data);

            BeginStock_Detail dataDetail;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                dataDetail = new BeginStock_Detail();
                dataDetail.ProductID = Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID"));
                dataDetail.UnitID = Convert.ToInt64(gridView1.GetRowCellValue(i, "UnitID"));
                dataDetail.BeginStockID = ID;
                dataDetail.OldQuantity = Convert.ToInt32(gridView1.GetRowCellValue(i, "OldQuantity"));
                dataDetail.NewQuantity = Convert.ToInt32(gridView1.GetRowCellValue(i, "NewQuantity"));
                RegisterService.RegisterServices.BeginStock_DetailService.Save(dataDetail);
            }
            this.Close();
            return ID;
        }
        private DataTable CreateTable()
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("ProductNameExt", typeof(string));
            tbl.Columns.Add("ProductID", typeof(int));
            tbl.Columns.Add("ProductCodeExt", typeof(string));
            tbl.Columns.Add("NewQuantityExt", typeof(int));
            tbl.Columns.Add("UnitNameExt", typeof(string));
            tbl.Columns.Add("UnitID", typeof(int));
            return tbl;
        }
        public List<long> VoucherSeletedProductID { get; set; }
        private void cmdaddproduct_Click(object sender, EventArgs e)
        {
            frmSelectProduct pro = new frmSelectProduct();

            VoucherSeletedProductID = new List<long>();
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                VoucherSeletedProductID.Add(Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID")));
            }
            
            if (pro.ShowDialog(VoucherSeletedProductID,1) == DialogResult.OK)
            {
                //IList<Product> listproduct = new List<Product>();
                IList<BeginStock_Detail> data = new List<BeginStock_Detail>();
                data = RegisterService.RegisterServices.BeginStock_DetailService.GetList_NewBeginByListPID(pro.ProductSeletedIDs);

                //listproduct = RegisterService.RegisterServices.ProductService.GetProductInListID(pro.ProductSeletedIDs);
                int newRowHandle;
                if (data != null)
                {
                    //IList<Product_Unit> listUnit = (IList<Product_Unit>)RegisterService.RegisterServices.ProductUnitService.GetList();
                    //if (listUnit.Count > 0)
                    //{
                    //    //repositoryItemLookUpEdit_Unit = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
                    //    repositoryItemLookUpEdit_Unit.DataSource = listUnit;
                    //}
                    //repositoryItemLookUpEdit_Unit.ValueMember = "ID";
                    //repositoryItemLookUpEdit_Unit.DisplayMember = "Name";
                    //repositoryItemLookUpEdit_Unit.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Đơn vị tính"));
                    foreach (var iproduct in data)
                    {
                        gridView1.AddNewRow();
                        newRowHandle = gridView1.FocusedRowHandle;
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductNameExt"], iproduct.ProductNameExt);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductID"], iproduct.ProductID);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["ProductCodeExt"], iproduct.ProductCodeExt);
                        gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["NewQuantityExt"], 0);
                        //lay danh sach don vi tinh
                        //IList<Product_UnitChange> listUnit = (IList<Product_UnitChange>)RegisterService.RegisterServices.ProductUnitChangeService.GetUnitChangeByProductID(iproduct.ID);

                       gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["UnitNameExt"], iproduct.UnitNameExt);
                       gridView1.SetRowCellValue(newRowHandle, gridView1.Columns["UnitID"], iproduct.UnitID);
                        gridView1.UpdateCurrentRow();
                    }
                    gridView1.RefreshData();
                    //gridView1.Focus();
                }
            }
        }

        private void cmd_save_Click(object sender, EventArgs e)
        {
            BeginStock beginstock = new BeginStock();
            beginstock.BeginStockDate = DateTime.Now;//Convert.ToDateTime(dateEdit_create.EditValue);
            beginstock.BeginStockNo = "KTTD";
            beginstock.StaffID = Convert.ToInt64(lookUpEdit_Staff.EditValue);
            beginstock.Note = "Khởi tạo tồn đầu kỳ";
            var data =(List<BeginStock>)RegisterService.RegisterServices.BeginStockService.GetList(" BeginStockNo ='KTTD'");
            long IDB=0;
            if (data.Count == 0)
            {
                IDB = RegisterService.RegisterServices.BeginStockService.Save(beginstock);
            }
            else IDB = data.FirstOrDefault().ID;
            if (gridView1.RowCount > 0)
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    BeginStock_Detail voucherDetail = new BeginStock_Detail();
                    voucherDetail.ProductID = Convert.ToInt64(gridView1.GetRowCellValue(i, "ProductID"));
                    voucherDetail.NewQuantity = Convert.ToInt32(gridView1.GetRowCellValue(i, "NewQuantityExt"));
                    voucherDetail.UnitID = Convert.ToInt64(gridView1.GetRowCellValue(i, "UnitID"));
                    voucherDetail.BeginStockID = IDB;
                    RegisterService.RegisterServices.BeginStock_DetailService.Save(voucherDetail);
                }
                MessageBox.Show("Thêm thành công");
                this.Close();
            }
            else
                MessageBox.Show("Chưa nhập danh sách hàng hóa");

        }

        private void frmStockBegin_Detail_Load(object sender, EventArgs e)
        {

        }
    }
}
